package net.wsfcp.android.ui.view.my.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.BillAdapter;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.bean.resultbean.BillBean;
import net.wsfcp.android.bean.resultbean.UserAssetsBean;
import net.wsfcp.android.http.mvp.contract.BillContract;
import net.wsfcp.android.http.mvp.presenter.BillPresenter;
import net.wsfcp.android.ui.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//我的账单
public class BillListActivity extends BaseActivity<BillPresenter> implements BillContract.BillView {

    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;

    @BindView(R.id.txt_bill_income)
    TextView txt_bill_income;//总收入
    @BindView(R.id.txt_bill_pay)
    TextView txt_bill_pay;//总支出

    @BindView(R.id.rv_bill)
    XRecyclerView rvBill;


    BillAdapter billAdapter;
    List<BillBean.BillList> billBeans = new ArrayList<BillBean.BillList>();
    int nowPage = 1;//当前页数
    int pageSize = 20;//每页加载个数

    @Override
    protected int getLayoutId() {
        return R.layout.activity_billlist;
    }

    @Override
    protected BillPresenter loadPresenter() {
        return new BillPresenter();
    }

    @Override
    protected void initData() {
        setmTitle(getString(R.string.fragment_my_bill));
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mPresenter.getTotalAssets();
        mPresenter.getBillList(nowPage, pageSize);

    }

    @Override
    protected void initListener() {
        setRefresh();
    }

    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;

        initRecyclerview();
        initAdapter();

    }

    public void initAdapter() {
        billAdapter = new BillAdapter(this, R.layout.item_list_bill, billBeans);
        rvBill.setAdapter(billAdapter);
        rvBill.setPullRefreshEnabled(false);//去掉下拉刷新
    }

    public void setRefresh() {
        rvBill.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                nowPage = 1;
                mPresenter.getBillList(nowPage, pageSize);
            }

            @Override
            public void onLoadMore() {
                nowPage++;
                mPresenter.getBillList(nowPage, pageSize);
            }
        });
    }


    public void initRecyclerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//设置布局管理器
        rvBill.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);

        //设置分隔线
        rvBill.addItemDecoration(new RecycleViewDivider(this, dp(8), dp(8), false));
////设置增加或删除条目的动画
//        rv_drawer.setItemAnimator( new DefaultItemAnimator());
        rvBill.setFootViewText("加载中...", "已获取所有账单");
    }


    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    public void getBillListSuccess(BillBean bean) {
        rvBill.refreshComplete();
        rvBill.loadMoreComplete();

        if (nowPage == 1) {
            billBeans.clear();
        }

        billBeans.addAll(bean.getData_list());
//        billAdapter.setOrderBean(orderBeans);
        billAdapter.notifyDataSetChanged();

        if (bean.getData_list().size() < pageSize) {
            rvBill.setNoMore(true);//没有更多了

        }
    }

    @Override
    public void getBillListFail(int code, String failMsg) {
        rvBill.refreshComplete();
        rvBill.loadMoreComplete();
        rvBill.setNoMore(true);
        billAdapter.notifyDataSetChanged();

    }

    @Override
    public void getTotalAssetsSuccess(UserAssetsBean bean) {
        txt_bill_income.setText("￥"+bean.getPurseBalance());
        txt_bill_pay.setText("￥"+bean.getTotalExpenditure());
    }

    @Override
    public void getTotalAssetsFail(String failMsg) {
        toast(failMsg);

    }
}
